package com.android.binderserverclient1

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import binderipc.IPassValue
import binderipc.PassValueConstant
import binderipc.PassValueProxy
import binderipcservicemanager.AdvancedServiceManagerConstant
import binderipcservicemanager.BCAdvancedServiceManagerProxy
import binderipcservicemanager.IBCAdvancedServiceManager
import com.android.binderserverclient1.databinding.ActivityMainBinding
import com.android.binderserverclient1.mmkv.BundleKeys
import com.android.binderserverclient1.mmkv.TestMMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity : BaseActivity<ActivityMainBinding>() {

    override fun getLayoutId() = R.layout.activity_main

    override fun init(savedInstanceState: Bundle?) {
//        val bundleResult = contentResolver.call(
//            Uri.parse(PassValueConstant.PASS_VALUE_URI),
//            PassValueConstant.ACTION_GET_SERVICE,
//            null,
//            null
//        )
//        var passValue : IPassValue? = null
//        if (bundleResult != null) {
//            val binder = bundleResult.getBinder(PassValueConstant.KEY_SERVER)
//            if (binder != null) {
//                passValue = PassValueProxy(binder, cacheDir)
//                Log.d(TAG,"value:${passValue.passInt(5)}")
//            }
//        }

        var mBCServiceManager: IBCAdvancedServiceManager? = null

        val bundleResult = contentResolver.call(
            Uri.parse(AdvancedServiceManagerConstant.SERVICE_MANAGER_URI),
            AdvancedServiceManagerConstant.ACTION_ADVANCED_GET_SERVICE,
            null,
            null
        )

        if (bundleResult != null) {
            val binder = bundleResult.getBinder(AdvancedServiceManagerConstant.KEY_ADVANCED_SERVER)
            if (binder != null) {
                mBCServiceManager = BCAdvancedServiceManagerProxy(binder)
            }
        }

        Log.d(TAG,"${mBCServiceManager?.getService("PassValueService") == null}")
        val passValue = mBCServiceManager?.getService("PassValueService")
            ?.let { PassValueProxy(it, cacheDir) }
        Log.d(TAG,"value:${passValue?.passInt(5)}")

        mBinding.btnPassFile.setOnClickListener {
            launch {
                withContext(Dispatchers.IO) {
                    passValue?.transactFileDescriptor()
                }
            }
        }

        mBinding.btnPassJpg.setOnClickListener {
            launch {
                withContext(Dispatchers.IO) {
                    //passValue?.getPFD()
                }
                Log.d(TAG, "value:${passValue?.passValueByOneWay(10, 20)}")
            }

            Log.d(TAG, "data0:${TestMMKV.getInstance().mmkv.decodeString(BundleKeys.TITLE, "no data")}")
            TestMMKV.getInstance().mmkv.encode(BundleKeys.TEST_FIRE, "hahaha")
            Log.d(TAG, "data0:${TestMMKV.getInstance().mmkv.decodeString(BundleKeys.TEST_FIRE, "no data")}")
        }

        mBinding.goToRemoteviews.setOnClickListener {
            startActivity(Intent(this@MainActivity, ShowRemoteViewsActivity::class.java))
        }

    }

    companion object {
        const val TAG = "BinderServerClient1"
    }
}